define(function (require) {
    require('bootstrapValidator');
    require('messager');
    require('table');
    require('typeahead');
    require('doctor-data');
    require('fileinput');
    require('daterangepicker');
    require('doctor-select');
    require('moment');
    require('clockpicker');
    require('datetimepicker');
    require('iCheck');

    $.module("BASE.ReportCalculation", function () {

        return {
            init: function () {
                this.loadEvent();
            },
            clearDatas:function () {
                var that = this;

                $("#_id").val(0);

                $('#fm').bootstrapValidator('resetForm', true);
                that.showDiv('main_div');
            },
            loadEvent: function () {
                var that = this;

                // 定义编辑框form
                var $form = $("#fm");

                // 查询
                $("#search-toolbar").on('click', "#btn_SPVoucher", function () {
                    let periodCode = $('#s_PeriodCode1').val(); //
                    if (periodCode == "") {
                        $.messager.popup("请输入会计期间编码，在执行", "warn");
                        return;
                    }
                    that.dwd_MCRS_MDM_sp_Voucher(periodCode);
                }).on('click', "#btn_SPVoucher6", function () {
                    let periodCode = $('#s_PeriodCode6').val(); //
                    if (periodCode == "") {
                        $.messager.popup("请输入会计期间编码，在执行", "warn");
                        return;
                    }
                    that.dwd_MCRS_MDM_sp_Voucher(periodCode);
                }).on('click', "#btn_SPSaleLines", function () {
                    let periodCode = $('#s_PeriodCode2').val();
                    if (periodCode == "") {
                        $.messager.popup("请输入会计期间编码，在执行", "warn");
                        return;
                    }
                    that.dwd_MCRS_BDM_sp_SaleLines(periodCode);
                }).on('click', "#btn_SPSaleLines4", function () {
                    let periodCode = $('#s_PeriodCode4').val();
                    if (periodCode == "") {
                        $.messager.popup("请输入会计期间编码，在执行", "warn");
                        return;
                    }
                    that.dwd_MCRS_BDM_sp_SaleLines(periodCode);
                }).on('click', "#btn_SPActualDataExtra", function () {
                    let periodCode = $('#s_PeriodCode3').val();
                    if (periodCode == "") {
                        $.messager.popup("请输入会计期间编码，在执行", "warn");
                        return;
                    }
                    that.dwd_MCRS_MDM_Sp_ActualDataExtra(periodCode);
                }).on('click', "#btn_SPInventoryBalance", function () {
                    let periodCode = $('#s_PeriodCode5').val();
                    if (periodCode == "") {
                        $.messager.popup("请输入会计期间编码，在执行", "warn");
                        return;
                    }
                    that.dwd_MCRS_MDM_Sp_ActualDataExtra(periodCode);
                }).on('click', "#btn_SPInOutCost", function () {
                    that.dwd_MCRS_Report_SP_In_Out_Cost();
                }).on('click', "#btn_SPInSales", function () {
                    that.dwd_MCRS_Report_SP_In_Sales();
                });

                // 返回
                $(".back_list").on('click', function () {
                    that.clearDatas();
                    that.showDiv('main_div');
                });

                //
                $('#fm').bootstrapValidator({
                    excluded: ':disabled',
                    feedbackIcons: {
                        valid: 'glyphicon glyphicon-ok',
                        invalid: 'glyphicon glyphicon-remove',
                        validating: 'glyphicon glyphicon-refresh'
                    },
                    fields: {
                        // title: {
                        //     validators: {
                        //         notEmpty: {
                        //             message: '此项必填'
                        //         }
                        //     }
                        // },
                        // order: {
                        //     validators: {
                        //         notEmpty: {
                        //             message: '此项必填'
                        //         },
                        //         regexp: {
                        //             regexp: /^[1-9]\d*$/i,
                        //             message: '格式不正确，必须大于0'
                        //         }
                        //     }
                        // }
                    }
                }).on('success.form.bv', function (e) {
                    e.preventDefault();

                    that.save(0);
                });

            },
            dwd_MCRS_MDM_sp_Voucher: function (periodCode= "") {
                var that = this;
                that.clearDatas();

                $("#indexMask").show();
                SYS.Core.ajax({
                    url: 'storedProcedure/dwd_MCRS_MDM_sp_Voucher',
                    common: {},
                    data: {
                        PeriodCode: periodCode
                    },
                    success: function (data) {
                        if (data.code == 1) {
                            $("#indexMask").hide();

                            $.messager.popup(data.msg, "success");
                            that.showDiv('main_div');
                        }
                    }
                });
            },
            dwd_MCRS_BDM_sp_SaleLines: function (periodCode= "") {
                var that = this;
                that.clearDatas();

                $("#indexMask").show();
                SYS.Core.ajax({
                    url: 'storedProcedure/dwd_MCRS_BDM_sp_SaleLines',
                    common: {},
                    data: {
                        PeriodCode: periodCode
                    },
                    success: function (data) {
                        if (data.code == 1) {
                            $("#indexMask").hide();

                            $.messager.popup(data.msg, "success");
                            that.showDiv('main_div');
                        }
                    }
                });
            },
            dwd_MCRS_MDM_Sp_ActualDataExtra: function (periodCode= "") {
                var that = this;
                that.clearDatas();

                $("#indexMask").show();
                SYS.Core.ajax({
                    url: 'storedProcedure/dwd_MCRS_MDM_Sp_ActualDataExtra',
                    common: {},
                    data: {
                        PeriodCode: periodCode
                    },
                    success: function (data) {
                        if (data.code == 1) {
                            $("#indexMask").hide();

                            $.messager.popup(data.msg, "success");
                            that.showDiv('main_div');
                        }
                    }
                });
            },
            dwd_MCRS_MDM_sp_InventoryBalance: function (periodCode= "") {
                var that = this;
                that.clearDatas();

                $("#indexMask").show();
                SYS.Core.ajax({
                    url: 'storedProcedure/dwd_MCRS_MDM_sp_InventoryBalance',
                    common: {},
                    data: {
                        PeriodCode: periodCode
                    },
                    success: function (data) {
                        if (data.code == 1) {
                            $("#indexMask").hide();

                            $.messager.popup(data.msg, "success");
                            that.showDiv('main_div');
                        }
                    }
                });
            },
            dwd_MCRS_Report_SP_In_Out_Cost: function () {
                var that = this;
                that.clearDatas();

                $("#indexMask").show();
                SYS.Core.ajax({
                    url: 'storedProcedure/dwd_MCRS_Report_SP_In_Out_Cost',
                    common: {},
                    data: {
                    },
                    success: function (data) {
                        if (data.code == 1) {
                            $("#indexMask").hide();

                            $.messager.popup(data.msg, "success");
                            that.showDiv('main_div');
                        }
                    }
                });
            },
            dwd_MCRS_Report_SP_In_Sales: function () {
                var that = this;
                that.clearDatas();

                $("#indexMask").show();
                SYS.Core.ajax({
                    url: 'storedProcedure/dwd_MCRS_Report_SP_In_Sales',
                    common: {},
                    data: {
                        PeriodCode: periodCode
                    },
                    success: function (data) {
                        if (data.code == 1) {
                            $("#indexMask").hide();

                            $.messager.popup(data.msg, "success");
                            that.showDiv('main_div');
                        }
                    }
                });
            },
            showDiv: function (div) {
                var divs = ['main_div','edit_div'];
                for(var i=0; i<divs.length; i++){
                    $("#" + divs[i]).hide();
                }
                $("#" + div).show();
            }
        }
    });
    BASE.ReportCalculation.init();
});